using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Jeffrey.Xuthus
{
    public class DataSettings
    {
        [CategoryAttribute("Spider Settings"), TypeConverter(typeof(EncodingConverter)), 
         DescriptionAttribute("Set the defualt Encoding when fetching the pages from Internet.")]
        public Encoding DefaultEncoding
        {
            get { return m_Encoding; }
            set { m_Encoding = value; }
        }

        [CategoryAttribute("Spider Settings"), DefaultValueAttribute(50),
         DescriptionAttribute("Set the max number of pages fetching from Internet.\r\nUnit: 10K (will be ignored when value is 0)")]
        public Int32 TotalPages
        {
            get { return m_TotalPages; }
            set { m_TotalPages = value; }
        }

        [CategoryAttribute("Spider Settings"), DefaultValueAttribute(3),
         DescriptionAttribute("Set the max depth when spider crawling.")]
        public Int32 Depth
        {
            get { return m_Depth; }
            set { m_Depth = value; }
        }

        [CategoryAttribute("Socket Settings"), DefaultValueAttribute(10),
         DescriptionAttribute("Set the sockets time-out for sending and receiving.\r\nUnit: second")]
        public Int32 TimeOut
        {
            get { return m_TimeOut; }
            set { m_TimeOut = value; }
        }

        [CategoryAttribute("Spider Settings"), DefaultValueAttribute(100),
         DescriptionAttribute("Set the idle time between two pages when spider fetching.\r\nUnit: millisecond")]
        public Int32 Interval
        {
            get { return m_Interval; }
            set { m_Interval = value; }
        }

        [CategoryAttribute("Socket Settings"), DefaultValueAttribute(3),
         DescriptionAttribute("Set the retrying times when TCP/IP buffer is blocked(the interval is 500ms).")]
        public Int32 BlockedRetry
        {
            get { return m_BlockedRetry; }
            set { m_BlockedRetry = value; }
        }

        [CategoryAttribute("Socket Settings"), DefaultValueAttribute(true),
        DescriptionAttribute("Whether or not to use the Dns Cache.")]
        public Boolean DnsCache
        {
            get { return m_DnsCache; }
            set { m_DnsCache = value; }
        }

        [CategoryAttribute("Socket Settings"), DefaultValueAttribute(true),
        DescriptionAttribute("Whether or not to use the persistent connection.")]
        public Boolean KeepAlive
        {
            get { return m_KeepAlive; }
            set { m_KeepAlive = value; }
        }

        [CategoryAttribute("System Settings"), DefaultValueAttribute(false),
         DescriptionAttribute("whether or not to force the garbage collection.\r\n(it's effectual when more than 2 spiders)")]
        public Boolean GarbageCollection
        {
            get { return m_GC; }
            set { m_GC = value; }
        }

        //the total number of pages to crawl
        private Int32 m_TotalPages;
        //the depth of pages to crawl
        private Int32 m_Depth;
        //the timeout for socket sending and receiving
        private Int32 m_TimeOut; //ms
        //the interval time for spider between two pages fetching
        private Int32 m_Interval;
        //the retry times when the TCP/IP buffer blocked...the interval is 500ms.
        private Int32 m_BlockedRetry;
        //whether or not to use the dns cache
        private Boolean m_DnsCache;
        //the default encoding to fetch the pages
        private Encoding m_Encoding;
        //whether or not to use the persistent connection
        private Boolean m_KeepAlive;
        //whether or not to enable the garbage collection
        private Boolean m_GC;

    }

    class EncodingConverter: TypeConverter
    {
        private static String defaultEncodingName = Encoding.Default.WebName;

        public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(new String[] { "ISO-8859-1", 
                                                               defaultEncodingName,
                                                               "ASCII",
                                                               "UTF-8",
                                                               "UTF-16",
                                                               "UTF-32"
                                                             }
                                               );   
        }
        public override Boolean CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return true;
        }
        public override Object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value, Type destinationType)
        {
            Encoding encoding = value as Encoding;

            if ( destinationType == typeof(String) &&
                 encoding != null
               )
            {
                return encoding.EncodingName;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override Boolean CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return true;
        }
        public override Object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            String name = value as String;

            if (name != null)
            {
                return Encoding.GetEncoding(name);
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}
